package com.itany.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.ResponseCodeConstant;
import com.itany.cms.entity.User;
import com.itany.cms.entity.UserParam;
import com.itany.cms.exception.CodeErrorException;
import com.itany.cms.exception.RequestParameterErrorException;
import com.itany.cms.exception.UserExistException;
import com.itany.cms.exception.UserNotExistException;
import com.itany.cms.service.UserService;
import com.itany.cms.util.ResponseResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public ModelAndView login(String loginName, String password, String role, String code, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        String image = (String) session.getAttribute("code");

        try {
            User user = userService.login(loginName, password, code, image);
            session.setAttribute("user", user);
            mav.setViewName("redirect:/showMain");
        } catch (RequestParameterErrorException e) {
            mav.addObject("loginMsg", e.getMessage());
            mav.setViewName("backend/back_login");
        } catch (CodeErrorException e) {
            mav.addObject("loginMsg", e.getMessage());
            mav.setViewName("backend/back_login");
        } catch (UserNotExistException e) {
            mav.addObject("loginMsg", e.getMessage());
            mav.setViewName("backend/back_login");
        } catch (Exception e) {
            e.printStackTrace();
            mav.addObject("loginMsg", "服务器内部异常");
            mav.setViewName("backend/back_login");
        }
        return mav;
    }
    @RequestMapping("findByParams")
    public  ModelAndView findByParams(User userParam, String pageNo, String pageSize){
        ModelAndView mav  = new ModelAndView();
        PageInfo<User> userPage = userService.findByParams(userParam,pageNo,pageSize);
        mav.addObject("userPage" ,userPage);
        mav.setViewName("backend/back_userSet");
        mav.addObject("userParam",userParam);
        return mav;
    }

    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id, String status) {
        ResponseResult result = new ResponseResult();
        try {
            userService.modifyStatus(id,status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/modifyName")
    public ResponseResult modifyName(String id, String nickname, String password, String role, String email) {
        ResponseResult result = new ResponseResult();
        try {
            userService.modifyName(id,nickname,password,role,email);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        }catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (UserExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

}